package com.perfma.xtest.tool;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.IgnoredErrorType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.lang.reflect.Field;

/**
 * @author: junhui.si
 * @date: 2021-10-22 17:32
 **/
public class IgnoreTextSheetWriteHandler implements SheetWriteHandler {
    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        try {
            // 忽略"以文本形式储存的数字"的告警检查
            Field sh = SXSSFSheet.class.getDeclaredField("_sh");
            sh.setAccessible(true);
            XSSFSheet xssfsheet = (XSSFSheet) sh.get(sheet);
            xssfsheet.addIgnoredErrors(new CellRangeAddress(0, 10000, 0, 1000), IgnoredErrorType.NUMBER_STORED_AS_TEXT);
        } catch (Exception e) {
           e.printStackTrace();
        }
    }
}
